/**
 * Created by Administrator on 2015/11/12.
 */
var exec = require("child_process").exec;
var querystring = require("querystring");
var fs = require("fs");
var formidable = require("formidable");

//处理"/start"请求
exports.start = function (request, response/*, postData*/) {
    var postData = '';
    request.on('data', function(data){
        postData+=data;
    });
    request.on('end', function(){
        var params = querystring.parse(postData);
        console.log('========客户端请求参数==========');
        for(var param in params){
            console.log(param +'---->'+ params[param]);
        }
    });
    console.log("Request handler 'start' was called.");
    /*exec("dir", function(error, stdout, stderr){
     response.writeHead(200, {"Content-Type":"text/plain"});
     response.write(stdout);
     response.end();
     });*/

    /*var body = '<html>' +
        '<head>' +
        '<meta http-equiv="Content-Type" content="text/html; ' +
        'charset=UTF-8" />' +
        '</head>' +
        '<body>' +
        '<form action="/upload" method="post">' +
        '<textarea name="text" rows="20" cols="60"></textarea>' +
        '<input type="submit" value="Submit text" />' +
        '</form>' +
        '</body>' +
        '</html>';

    response.writeHead(200, {"Content-Type": "text/html;charset=utf-8"});
    response.write(body);
    response.end();*/

    var body =
        '<html>'+
        '<head>'+
        '<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />'+
        '</head>'+
        '<body>'+
        '<form action="/upload" enctype="multipart/form-data" method="post">'+
        '<input type="file" name="upload" multiple="multiple">'+
        '<input type="submit" value="Upload file" />'+
        '</form>'+
        '</body>'+
        '</html>';

    response.writeHead(200, {"Content-Type":"text/html;charset=utf-8"});
    response.write(body);
    response.end();
}

//线程睡眠指定的时间
function sleep(millseconds) {
    var startTime = new Date().getTime();
    while (new Date().getTime() < startTime + millseconds);
}

//处理"/upload"请求
exports.upload = function (request, response/*, postData*/) {
    console.log("Request handler 'upload' was called.");
    /*response.writeHead(200, {'Content-Type': 'text/plain;charset=UTF-8'});
    response.write("You've sent:" + querystring.parse(postData).text);
    response.end();*/

    var form = new formidable.IncomingForm();
    form.uploadDir = "upload";
    console.log("about to parse");
    form.parse(request, function(error, fields, files){
        console.log("parse done");
        var newPath = "upload/test.png";
        fs.renameSync(files.upload.path, newPath);
        response.writeHead(200, {"Content-Type":"text/html;charset=utf-8"});
        response.write("reveived image:<br/>");
        //response.write("<img src='/show' />");
        response.write("<a href='/show'>显示图片</a>");
        response.end();
    });
}

//显示文件
exports.show = function(request, response/*, postData*/){
    console.log("Request handler 'show' was called.");
    fs.readFile("upload/test.png", "binary", function(error, file){
        if(error){
            response.writeHead(500, {"Content-Type":"text/plain"});
            response.write(error + "\n");
            response.end();
        }else{
            response.writeHead(200, {"Content-Type":"image/png"});
            response.write(file, "binary");
            response.end();
        }
    });
}